<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * goodsimg short summary.
 *
 * goodsimg description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Goodsimg extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Color_model');
        $this->load->Model('Goods_img_model');
        $this->load->library('form_validation');
    }

    public function goodsimglist()
    {
        $data["goods_id"]=$this->uri->segment(4);
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        $pagesize=10;
        $one=$this->Goods_img_model->get_page($cupge,$pagesize,"*,c.colorkey as colorkey,c.title as title","goods_img","order by goods_img_id asc","goods_img_id","where goods_id=".$data["goods_id"],"LEFT join color c on c.color_id=a.goods_color_id");
        $this->load->library('page');
        $p=new Page();
        $data['goodsimgList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/goodsimg/goodsimglist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/goodsimg/goodsimglist.html',$data);

    }

    public function addgoodsimg()
    {
        $data["goods_id"]=$this->uri->segment(4);
        $this->load->Model('Goods_color_model');
        $data['goodscolorList']=$this->Goods_color_model->get_all_goods_color_by_goods_id($data["goods_id"]);
        $this->load->view('Admin/goodsimg/addgoodsimg.html',$data);
    }

    public function add_goodsimg()
    {
        $valiRes= $this->form_validation->run('goodsimg_vali');
        $this->load->library('ajaxMsgModel');
        $goodsimgModel= new Goods_img_model();
        if($valiRes)
        {
            $goodsimgModel->imgurl=$this->input->post('imgurl');
            $goodsimgModel->goods_id=$this->input->post('goods_id');
            $goodsimgModel->goods_color_id=$this->input->post('goods_color_id');
            $goodsimgModel->thumbnail_img=$this->input->post('thumbnail_img');
            $res= $goodsimgModel->insert_goods_img($goodsimgModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/goodsimg/goodsimglist/".$goodsimgModel->goods_id);
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editgoodsimg()
    {
        $goodsimgId=$this->uri->segment(4);
    	$data['goodsimgModel']=$this->Goods_img_model->get_goods_img_by_id($goodsimgId);
        $this->load->Model('Goods_color_model');
        $data['goodscolorList']=$this->Goods_color_model->get_all_goods_color_by_goods_id($data['goodsimgModel']->goods_id);
        $this->load->view('Admin/goodsimg/editgoodsimg.html',$data);
    }

    public function edit_goodsimg()
    {
        $valiRes= $this->form_validation->run('goodsimg_vali');
        $this->load->library('ajaxMsgModel');
        $goodsimgModel= new Goods_img_model();
        if($valiRes)
        {
            $goodsimgModel->goods_img_id=$this->input->post('goods_img_id');
            $goodsimgModel->imgurl=$this->input->post('imgurl');
            $goodsimgModel->goods_id=$this->input->post('goods_id');
            $goodsimgModel->goods_color_id=$this->input->post('goods_color_id');
            $goodsimgModel->thumbnail_img=$this->input->post('thumbnail_img');
            $res= $goodsimgModel->edit_goods_img($goodsimgModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/goodsimg/goodsimglist/".$goodsimgModel->goods_id);
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function del_goodsimg()
    {
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $res=$this->Goods_img_model->del_goods_img_byid($this->uri->segment(4));
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batchdel_goodsimg()
    {
        $goodsimg_id=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $goodsimgModel=new $this->Goods_img_model();
        $goodsimg_idArray=explode('|',trim($goodsimg_id,'|'));
        $res=0;
        if(count($goodsimg_idArray)>0)
        {
            $res=$goodsimgModel->batchdel_goods_img($goodsimg_idArray);
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}